package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import db.DBUtil;
import model.Topic;

public class TopicDAO {
	public Topic getTopicById(int topicId){
		try{
			Connection conn = DBUtil.getConnection();
			PreparedStatement stmt = conn.prepareStatement("select * from topic where id = ?");
			stmt.setInt(1, topicId);
			ResultSet rs = stmt.executeQuery();
			if(rs.next()){
				Topic topic = new Topic(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5), rs.getInt(6), rs.getString(7));
				return topic;
			}else{
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public int addTopic(Topic topic){
		try{
			Connection conn = DBUtil.getConnection();
			PreparedStatement stmt = conn.prepareStatement("insert into topic (content,zhangjie,type,teacherid,mark,answer) values(?,?,?,?,?,?)");
			stmt.setString(1, topic.getContent());
			stmt.setString(2, topic.getZhangjie());
			stmt.setInt(3, topic.getType());
			stmt.setInt(4, topic.getTeacherid());
			stmt.setInt(5, topic.getMark());
			stmt.setString(6, topic.getAnswer());
			stmt.execute();
			ResultSet rs = stmt.getGeneratedKeys();
			if(rs.next()){
				return rs.getInt(1);
			}
			return -1;
		}catch(Exception e){
			e.printStackTrace();
			return -1;
		}
	}
	public List<Topic> getTopicsByTypeAndTeacherId(int type,int teacherId){
		List<Topic> list = new ArrayList<Topic>();
		try{
			Connection conn = DBUtil.getConnection();
			PreparedStatement stmt = conn.prepareStatement("select * from topic where type=? and teacherid=?");
			stmt.setInt(1, type);
			stmt.setInt(2, teacherId);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				Topic topic = new Topic();
				topic.setAnswer(rs.getString("answer"));
				topic.setContent(rs.getString("content"));
				topic.setId(rs.getInt("id"));
				topic.setMark(rs.getInt("mark"));
				topic.setTeacherid(rs.getInt("teacherid"));
				topic.setType(type);
				topic.setZhangjie(rs.getString("zhangjie"));
				list.add(topic);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
	public boolean deleteByTopicId(int topicId){
		try{
			Connection conn = DBUtil.getConnection();
			PreparedStatement stmt = conn.prepareStatement("delete from topic where id=?");
			stmt.setInt(1, topicId);
			stmt.execute();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public boolean updateTopic(Topic topic) {
		try{
			Connection conn = DBUtil.getConnection();
			PreparedStatement stmt = conn.prepareStatement("update topic set id=?, content=?,zhangjie=?,type=?,mark=?,answer=? where id=?");
			stmt.setInt(1, topic.getId());
			stmt.setString(2, topic.getContent());
			stmt.setString(3, topic.getZhangjie());
			stmt.setInt(4, topic.getType());
			stmt.setInt(5, topic.getMark());
			stmt.setString(6,topic.getAnswer());
			stmt.setInt(7,  topic.getId());
			stmt.execute();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}
